package com.airbat.prize.modules.base.service.resource.impl;

import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import com.airbat.prize.common.pojo.PageResult;
import com.airbat.prize.modules.base.controller.resource.vo.*;
import com.airbat.prize.modules.base.convert.resource.BaseResourceConvert;
import com.airbat.prize.modules.base.dal.dataobject.resource.BaseResourceDO;
import com.airbat.prize.modules.base.dal.mysql.resource.BaseResourceMapper;
import com.airbat.prize.modules.base.service.resource.BaseResourceService;
import com.airbat.prize.modules.infra.framework.file.config.FileProperties;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 文件资源 Service 实现类
 *
 * @author 芋艿
 */
@Service
@Validated
public class BaseResourceServiceImpl implements BaseResourceService {

    @Resource
    private BaseResourceMapper resourceMapper;
    @Resource
    private FileProperties fileProperties;

    @Override
    public String createFile(String fileName, int type, byte[] content) {
        //保存文件
        FileUtil.writeBytes(content, fileProperties.getBasePath() + fileName);

        // 保存到数据库
        BaseResourceDO file = new BaseResourceDO();
        file.setEditTime(new Date());
        file.setPath(fileProperties.getBasePath() + fileName);
        file.setName(fileName);
        file.setSize(content.length);
        file.setSuffixes(FileTypeUtil.getType(new ByteArrayInputStream(content)));
        file.setType(type);
        resourceMapper.insert(file);
        // 拼接路径返回
        return file.getId();
    }

    @Override
    public String createResource(BaseResourceCreateReqVO createReqVO) {
        // 插入
        BaseResourceDO resource = BaseResourceConvert.INSTANCE.convert(createReqVO);
        resourceMapper.insert(resource);
        // 返回
        return resource.getId();
    }

    @Override
    public void updateResource(BaseResourceUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateResourceExists(updateReqVO.getId());
        // 更新
        BaseResourceDO updateObj = BaseResourceConvert.INSTANCE.convert(updateReqVO);
        resourceMapper.updateById(updateObj);
    }

    @Override
    public void deleteResource(String id) {
        // 校验存在
        this.validateResourceExists(id);
        // 删除
        resourceMapper.deleteById(id);
    }

    private void validateResourceExists(String id) {
        if (resourceMapper.selectById(id) == null) {
//            throw exception(RESOURCE_NOT_EXISTS);
        }
    }

    @Override
    public BaseResourceDO getResource(String id) {
        return resourceMapper.selectById(id);
    }

    @Override
    public List<BaseResourceDO> getResourceList(Collection<String> ids) {
        return resourceMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BaseResourceDO> getResourcePage(BaseResourcePageReqVO pageReqVO) {
        return resourceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BaseResourceDO> getResourceList(BaseResourceExportReqVO exportReqVO) {
        return resourceMapper.selectList(exportReqVO);
    }

}
